﻿using Grpc.Core;
using Grpc.Core.Interceptors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Infrastructure.Share.Tools;

namespace Windows.GrpcService.Share
{
    public class ExceptionHandlerInterceptor: Interceptor
    {
        public override async Task<TResponse> UnaryServerHandler<TRequest, TResponse>(
            TRequest request,
            ServerCallContext context,
            UnaryServerMethod<TRequest, TResponse> continuation)
        {
            try
            {
                return await continuation(request, context);
            }
            catch (RpcException ex)
            {
                Logger.Error("请求处理异常" + ex.Message, ex);
                throw ex;
            }
            catch (Exception ex)
            {
                Logger.Error("请求处理异常" + ex.Message, ex);
                throw new RpcException(new Status(StatusCode.Unknown, StatusCode.Unknown.ToString()));
            }
        }

    }
}
